import Basic from "./basic";
import { mapKeys } from "../../util";

export default class Geolocation {
    constructor (parent, bol = true) {
        this.basic = parent && bol ? parent : new Basic();
        this.Geolocation = null;
    }

    init (opts = {}) {
        const vm = this;
        const basic = this.basic;
        return new Promise(async resolve => {
            await basic.loadMapApi();
            await basic.plugin('AMap.Geolocation', {}, false);
            if (vm.Geolocation) return resolve();
            const geolocation = new basic.AMap.Geolocation({
                // 是否使用高精度定位，默认：true
                enableHighAccuracy: true,
                // 设置定位超时时间，默认：无穷大
                timeout: 5000,
                // 定位按钮的停靠位置的偏移量，默认：Pixel(10, 20)
                buttonOffset: new basic.AMap.Pixel(10, 20),
                //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                zoomToAccuracy: true,
                //  定位按钮的排放位置,  RB表示右下
                buttonPosition: 'RB',
                ...opts
            });
            basic.map.addControl(geolocation);
            vm.Geolocation = geolocation;
            resolve()
        });
    }

    getCurrentPosition (opts) {
        const vm = this;
        return new Promise(async resolve => {
            if (!vm.Geolocation) await vm.init(opts);
            else {

            }
            if (!vm.Geolocation) return resolve(false);
            vm.Geolocation.getCurrentPosition(function (status, result) {
                if (status === 'complete') {
                    const obj = mapKeys(result.addressComponent, {}, [
                        'city',
                        'citycode',
                        'province',
                        'adcode'
                    ]);
                    const res = {
                        position: result.position,
                        longitude: result.position && result.position.lng,// 经度
                        latitude: result.position && result.position.lat,// 纬度
                        address: result.formattedAddress,
                        addressComponent: result.addressComponent,
                        type: result.location_type,
                        ...obj
                    };
                    resolve(res)
                } else {
                    resolve(false)
                }
            })
        })
    }

    async getCityInfo (opts) {
        const vm = this;
        if (!vm.Geolocation) await vm.init(opts);
        else {
        }
        return new Promise(resolve => {
            if (!vm.Geolocation) return resolve(false);
            vm.Geolocation.getCityInfo(function (status, result) {
                if (status === 'complete') {
                    const res = {
                        longitude: result.center && result.center[0],
                        latitude: result.center && result.center[1],
                        city: result.city,
                        province: result.province,
                        citycode: result.citycode,
                        adcode: result.adcode,
                    };
                    resolve(res)
                } else {
                    resolve(false)
                }
            })
        })
    }
}
